layui.use(['form','treeSelect','layer','upload','laydate'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;
    var laydate = layui.laydate;

    //回显数据
    var request = GetRequest();
    $.get("/api/agt_agritech/" + request.id,function (data) {

        $("#id").val(data.id);
        $("#pid").val(data.pid);
        $("#agritechTitle").val(data.agritechTitle);
        $("#agritechAbstract").val(data.agritechAbstract);
        $("#agritechContent").val(data.agritechContent);
        $("#agritechCover").val(data.agritechCover);
        $("#createdBy").val(data.createdBy);
        $("#createdTime").val(data.createdTime);
        $("#updatedTime").val(data.updatedTime);

        //处理日期时间信息
        laydate.render({
            elem: '#createdTime'
            ,type: 'datetime'
            ,value: new Date(data.createdTime) //必须遵循format参数设定的格式
        });
        laydate.render({
            elem: '#updatedTime'
            ,type: 'datetime'
            ,value: new Date(data.updatedTime) //必须遵循format参数设定的格式
        });

        //所属物种
        treeSelect.render({
            elem: '#tree-select-species',
            data: '/api/agt_species/tree/select',
            type: 'get',
            placeholder: '请选择物种分类',// 占位符
            search: true,// 是否开启搜索功能：true/false，默认false
            click: function(data){
                $("#tree-select-species").val(data.current.id);
            },
            success: function (item) { // 加载完成后的回调函数 areaId
                if (data.pid != null && data.pid != "") {
                    $("#tree-select-species").val(data.pid);
                } else {
                    $("#tree-select-species").val("0");
                }
                treeSelect.checkNode('tree-select-species',data.pid);
            }
        });

    });

    //修改物种信息
    form.on('submit(edit)', function(data){
        data.field.createdTime = new Date(data.field.createdTime);
        data.field.updatedTime = new Date(data.field.updatedTime);
        console.log(data);
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/api/agt_agritech/edit',
            data:data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
});